/*
 * [程序名称] Weather auto sender
 * [参考资料] http://code.google.com/p/auto-weather-sender/ 
 * [开源协议] Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.joe.weather;

import java.util.Calendar;
import java.util.TimeZone;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

import com.joe.weather.service.WeatherService;

public class BootReceiver extends BroadcastReceiver
{
	private static final String TAG = BootReceiver.class.getSimpleName();
	
	private WeatherApplication application = null;
	
	private boolean systemEnable;

	// Launch the weather service when system boot
	@Override
	public void onReceive(Context context, Intent intent)
	{
		Log.e(TAG, "Running into BootReceiver......");
		
		application = (WeatherApplication) context.getApplicationContext();
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
		
		systemEnable = sp.getBoolean("systemSwitch", true);
		
		if(systemEnable)
		{
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
			
			final int sendAtHour = sp.getInt(application.SEND_AT_HOUR, application.DEFAULT_SEND_AT_HOUR);
			if(sendAtHour <= calendar.get(Calendar.HOUR_OF_DAY))
			{
				Intent mIntent = new Intent(context, WeatherService.class);
				Bundle mBundle = new Bundle();
				mBundle.putBoolean(application.REPEATFLAG, true);
				mIntent.putExtras(mBundle);
				mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startService(mIntent);
				
				Log.e(TAG, "start weather service now.....");
			}
			else
			{
				calendar.set(Calendar.HOUR_OF_DAY, sendAtHour);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				
				Intent mIntent = new Intent(context, WeatherService.class);
				Bundle mBundle = new Bundle();
				mBundle.putBoolean(application.REPEATFLAG, true);
				mIntent.putExtras(mBundle);
				PendingIntent pendingIntent = PendingIntent.getService(context, 0, mIntent, 0);
				
				AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
				alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
				Log.e(TAG, "the service will start at " + sendAtHour + ":00");
				
				Calendar c2 = Calendar.getInstance();
				long l = (calendar.getTimeInMillis() - c2.getTimeInMillis())/1000;
				
				Log.e(TAG, "time difference : " + l);
			}
		}
	}
}
